package com.crm.controller;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.crm.base.ResponseResult;
import com.crm.domain.dto.ContractDto;
import com.crm.domain.dto.QueryContract;
import com.crm.domain.entity.Contract;
import com.crm.service.ContractService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

/**
 * @author Yujia,Xanny
 */
@RestController
@RequiredArgsConstructor
@Api(tags = "合同管理")
@RequestMapping("/contract")
public class ContractController {

    private final ContractService contractService;

    /**
     * 分页查询合同信息
     * @param queryContract
     * @return
     */
    @GetMapping("/list")
    @ApiOperation(value = "分页查询合同信息")
    public ResponseResult selectByPage(QueryContract queryContract){
        Page<Contract> pageResponse = contractService.selectByPage(queryContract);
        return ResponseResult.success()
                .put("total",pageResponse.getTotal())
                .put("rows",pageResponse.getRecords());
    }

    /**
     * getById
     * @param id
     * @return
     */
    @GetMapping("{id}")
    @ApiOperation(value = "根据ID查询合同信息")
    public ResponseResult selectById(@PathVariable("id") Long id){
        Contract contract = contractService.lambdaQuery().eq(Contract::getId, id).one();
        return ResponseResult.success(contract);
    }

    @PostMapping
    @ApiOperation(value = "新增合同信息")
    public ResponseResult addContract(@RequestBody ContractDto contractDto){
        Contract contract = BeanUtil.copyProperties(contractDto, Contract.class);
        contractService.save(contract);
        return ResponseResult.success();
    }

    @PutMapping
    @ApiOperation(value = "修改合同信息")
    public ResponseResult updateContract(@RequestBody ContractDto contractDto) {
        Contract contract = BeanUtil.copyProperties(contractDto, Contract.class);
        contractService.updateById(contract);
        return ResponseResult.success();
    }

    @GetMapping("/detail/{id}")
    @ApiOperation(value = "根据ID查询合同详情")
    public ResponseResult detail(@PathVariable("id") Long id){
        Contract contract = contractService.lambdaQuery().eq(Contract::getId, id).one();
        return ResponseResult.success(contract);
    }
}
